import matplotlib.pyplot as plt
from rest_framework.response import Response
from Home.models import OrdersModel
import pymysql
def draw(request):
    con=pymysql.connect(port=3306,host="localhost",user="root",password="123456",charset="utf8",database="fuguang")
    c1=con.cursor()
    sql="SELECT DATE_FORMAT(created_time, '%m') AS month, SUM(tmoney) AS total_money FROM `order` GROUP BY DATE_FORMAT(created_time, '%m') "
    data=c1.execute(sql)

    data = c1.fetchall()
    print(data)
    plt.figure(figsize=(10, 10), dpi=100)
    list1=[]
    list2=[]
    for i in data:
        list1.append(i[0])
        list2.append(i[1])
    plt.plot(list1, list2)
    plt.show()
    return Response("ok")

def getMonth(request):
    con=pymysql.connect(port=3306,host="localhost",user="root",password="123456",charset="utf8",database="fuguang")
    c1=con.cursor()
    # sql="SELECT MONTH(STR_TO_DATE(created_time, '%d/%m/%Y')) month,count(*) count from testphone WHERE success=1 GROUP BY   MONTH(STR_TO_DATE(created_time, '%d/%m/%Y'))"

    sql = "SELECT MONTH(STR_TO_DATE(created_time, '%d/%m/%Y')) month, 1 as count from testphone WHERE success=1 "

    data=c1.execute(sql)

    data = c1.fetchall()

    return data


